package com.scope.imageTextReader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import com.scope.activities.PreprocessActivity;
import com.scope.imageFilters.FilterManager;

import java.io.FileOutputStream;


/**
 * Created by Samrith on 12/10/13.
 */
public class PreProcessAsync extends AsyncTask<FilterManager, Object, Bitmap> {
    public PreProcessAsync(String imagePath, PreprocessActivity preprocess) {
        mImagePath = imagePath;
        mPreprocess = preprocess;
    }

    private String mImagePath;
    private Bitmap mImage;
    private PreprocessActivity mPreprocess;

    @Override
    protected void onPreExecute() {
        mImage = BitmapFactory.decodeFile(mImagePath);
        mPreprocess.asyncProgress(0, mImage, "");
    }

    public final static String Tag = "SCOPE 2";

    @Override
    protected Bitmap doInBackground(FilterManager... filters) {
        Bitmap image = mImage;
        for (int i = 0; i < filters.length; i++) {
            Log.v(Tag, " " + i + " publishing " + filters[i].getFilterName());
            publishProgress(i + 1, image, filters[i].getFilterName());
            try {
                image = filters[i].applyFilter(image);
            } catch (Exception e) {
            }
            Log.v(Tag, "applied filter...");        	
        }
        return image;
    }

    @Override
    protected void onProgressUpdate(Object... values) {
    	mPreprocess.asyncProgress((Integer)values[0], (Bitmap)values[1], (String)values[2]);
    }
    
    @Override
    protected void onPostExecute(Bitmap bitmap) {
    	mPreprocess.asyncEnd(bitmap);
    }
}
